package com.coca1cole.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.coca1cole.auth.application.convert.AuthUserRoleDTOConverter;
import com.coca1cole.auth.application.dto.AuthUserRoleDTO;
import com.coca1cole.auth.domain.entity.AuthUserRoleBO;
import com.coca1cole.auth.domain.service.AuthUserRoleDomainService;
import com.coca1cole.auth.entity.Result;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * controller
 *
 * @author coca1cole
 * @since 2024-01-22 18:32:46
 */
@RestController
@RequestMapping("/userRole/")
@Slf4j
public class AuthUserRoleController {

    @Resource
    private AuthUserRoleDomainService authUserRoleDomainService;

    /**
     * 新增
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthUserRoleDTO authUserRoleDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserRoleController.add.dto:{}", JSON.toJSONString(authUserRoleDTO));
            }
            Preconditions.checkNotNull(authUserRoleDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(authUserRoleDTO.getRoleId(), "不能为空");
            AuthUserRoleBO AuthUserRoleBO = AuthUserRoleDTOConverter.INSTANCE.convertDTOToBO(authUserRoleDTO);
            return Result.ok(authUserRoleDomainService.add(AuthUserRoleBO));
        } catch (Exception e) {
            log.error("AuthUserRoleController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增失败");
        }

    }

    /**
     * 修改
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserRoleDTO authUserRoleDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserRoleController.update.dto:{}", JSON.toJSONString(authUserRoleDTO));
            }
            Preconditions.checkNotNull(authUserRoleDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(authUserRoleDTO.getRoleId(), "不能为空");
            AuthUserRoleBO authUserRoleBO = AuthUserRoleDTOConverter.INSTANCE.convertDTOToBO(authUserRoleDTO);
            return Result.ok(authUserRoleDomainService.update(authUserRoleBO));
        } catch (Exception e) {
            log.error("AuthUserRoleController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新信息失败");
        }

    }

    /**
     * 删除
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserRoleDTO authUserRoleDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserRoleController.delete.dto:{}", JSON.toJSONString(authUserRoleDTO));
            }
            Preconditions.checkNotNull(authUserRoleDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(authUserRoleDTO.getRoleId(), "不能为空");
            AuthUserRoleBO authUserRoleBO = AuthUserRoleDTOConverter.INSTANCE.convertDTOToBO(authUserRoleDTO);
            return Result.ok(authUserRoleDomainService.delete(authUserRoleBO));
        } catch (Exception e) {
            log.error("AuthUserRoleController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除信息失败");
        }

    }

}
